package com.jin290.mqlibrary;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by MQ on 2016/9/18.
 */
public class CourseDialogAdapter extends BaseAdapter {
    private Context mContext;
    private List<String>mList;
    private int mposition=10086;


    public CourseDialogAdapter(Context context) {
        mContext = context;
    }

    public void getdata(List<String> l)
    {
        mList=l;
    }

    public void getc(int i)
    {
        mposition=i;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        Viewh viewh;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_course_dialog, null);
            viewh = new Viewh();

            viewh.tv_course_dialog= (TextView) view.findViewById(R.id.tv_course_dialog);
            viewh.delete_image_view= (ImageView) view.findViewById(R.id.delete_image_view);

            view.setTag(viewh);
        } else {
            viewh = (Viewh) view.getTag();//取出ViewHolder对象
        }


        viewh.tv_course_dialog.setText(mList.get(position));
        viewh.delete_image_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mList.remove(position);
                String s=listToString1(mList);
                SPUtils.put(mContext,"class",s);
                notifyDataSetChanged();

            }
        });


        if(position<8)
        {
            viewh.delete_image_view.setVisibility(View.GONE);
        }
        else if(position==(mList.size()-1))
        {
            viewh.delete_image_view.setVisibility(View.GONE);
        }
        else {
            viewh.delete_image_view.setVisibility(View.VISIBLE);
        }


        if(mposition==position)
        {
            viewh.tv_course_dialog.setBackgroundResource(R.drawable.course1);
            viewh.tv_course_dialog.setTextColor(mContext.getResources().getColor(R.color.red_btn_bg_color));
        }
        else {
            viewh.tv_course_dialog.setBackgroundResource(R.drawable.course);
            viewh.tv_course_dialog.setTextColor(mContext.getResources().getColor(R.color.black));
        }




        return view;
    }

    public void setMposition(int mposition){
        this.mposition = mposition;
    }
    public int getMposition(){
        return mposition;
    }


    public class Viewh {

        TextView tv_course_dialog;
        ImageView delete_image_view;
    }


    /***
     * List转String
     * @param stringList
     * @return
     */
    public static String listToString1(List<String> stringList){
        if (stringList==null) {
            return null;
        }
        StringBuilder result=new StringBuilder();
        boolean flag=false;
        for (String string : stringList) {
            if (flag) {
                result.append(",");
            }else {
                flag=true;
            }
            result.append(string);
        }
        return result.toString();
    }

}
